package cn.myframe.controller.sys;

import cn.myframe.controller.AbstractController;
import cn.myframe.controller.BaseController;
import cn.myframe.entity.sys.SysBaseDataEntity;
import cn.myframe.service.BaseService;
import cn.myframe.service.sys.SysBaseDataService;
import cn.myframe.service.sys.SysUserService;
import cn.myframe.task.init.BaseDataInit;
import cn.myframe.utils.core.R;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.*;


/**
 * 基础类型表
 * 
 * @author ynz
 * @email ynz@myframe.cn
 * @date 2017-08-14 14:20:08
 */
@RestController
@RequestMapping("/sys/sysbasedata")
public class SysBaseDataController   extends AbstractController implements BaseController<SysBaseDataEntity,BaseService<SysBaseDataEntity>>{
	@Autowired
	private SysBaseDataService sysBaseDataService;
	@Autowired
	private SysUserService sysUserService;

	@Value("${application.version}")
	private String appVersionInfo;

	/**
	 * 列表
	 */
	@ResponseBody
	@RequestMapping(value = "/list",method = {RequestMethod.GET})
	@RequiresPermissions("sysbasedata:list")
	public R list(@RequestParam Map<String, Object> params){
		return this.defaultList(params);
	}	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("sysbasedata:info")
	public R info(@PathVariable("id") Long id){
		return this.defaultInfo(id,"sysBaseData");
	}
	
	/**
	 * 新增保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("sysbasedata:save")
	public R save(@RequestBody SysBaseDataEntity sysBaseData){
		sysBaseDataService.save(sysBaseData);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("sysbasedata:delete")
	public R delete(@RequestBody Long[] ids){		
		return this.defaultDelete(ids);
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")	
	@RequiresPermissions("sysbasedata:update")
	public R update(@RequestBody SysBaseDataEntity sysBaseData){		
		return this.defaultUpdate(sysBaseData);
	}
	
   /**
    * 提交,审核
    */
	@RequestMapping("/audit")	
	@RequiresPermissions(value={"sysbasedata:submit","sysbasedata:auditpass","sysbasedata:auditnopass"},logical=Logical.OR)
	public R audit(@RequestBody Long[] ids ,@RequestParam Integer status){
		return this.defaultAudit(ids, status);
	}
	
   /**
	* 检验字段值重复
	*/
    @RequestMapping("/checkExist")
    @ResponseBody
    public String checkExist(String id,String value,String columnName){
        return this.defaultCheckExist(id, value, columnName);
    }
    
   
   /**
    * 获取相对应的基础数据
    */
   @RequestMapping("/getAllBaseData")
   public R getAllBaseData(String language){

	   Map<String, List<SysBaseDataEntity>>  BASE_DATA_MAP = BaseDataInit.I18n_Date_MAP.get(language);
	   String yearList = "yearList";

       Map<String, String> yearListMap = new HashMap<>();
       List<SysBaseDataEntity> list = new ArrayList<SysBaseDataEntity>();
       
       Calendar calendar = Calendar.getInstance();
       int year = calendar.get(Calendar.YEAR);//得到年
       if("zh-CN".equals(language)){
           for(int i = 0;i < 3;i++){
               SysBaseDataEntity sysBaseDataEntity = new SysBaseDataEntity();
               sysBaseDataEntity.setType(yearList);
               sysBaseDataEntity.setKey(String.valueOf(year));
               sysBaseDataEntity.setValue(year + "年");
               sysBaseDataEntity.setLanguage("zh-CN");
               list.add(sysBaseDataEntity);
               year++;
           }           
       }else{
           for(int i = 0;i < 3;i++){
               SysBaseDataEntity sysBaseDataEntity = new SysBaseDataEntity();
               sysBaseDataEntity.setType(yearList);
               sysBaseDataEntity.setKey(String.valueOf(year));
               sysBaseDataEntity.setValue(year + "Year");
               sysBaseDataEntity.setLanguage("en-US");
               list.add(sysBaseDataEntity);
               year++;
           }                      
       }
       BASE_DATA_MAP.put(yearList, list);
	   // 系统版本信息
	   List<SysBaseDataEntity> pageDataList = BASE_DATA_MAP.get("pageData");
	   SysBaseDataEntity versionEntity = new SysBaseDataEntity();
	   versionEntity.setKey("appVersionInfo");
	   versionEntity.setValue(appVersionInfo);
	   pageDataList.add(versionEntity);

       return R.ok().put("allBaseDataMap", BASE_DATA_MAP);
   }



	@Override
	public BaseService<SysBaseDataEntity> getService() {
		// TODO Auto-generated method stub
		return sysBaseDataService;
	}
	    
	
}
